//
//  VisotorView.swift
//  Weibo
//
//  Created by 唐润欣 on 16/6/17.
//  Copyright © 2016年 唐润欣. All rights reserved.
//

import UIKit

///  击点访客视loginButton的代理
protocol VisitorDelegate {
    
    func didLogin()
}

// MARK: - visitorView的生命周期、属性、构造函数
class VisitorView: UIView {
    
    /// 代理
    var delegate: VisitorDelegate?
    
    //添加子控件： 从上往下，从左往右 
    //图标
    lazy var iconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    
    //转圈图片
    lazy var circleView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    
    //遮罩图片
    lazy var maskIconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    
    //文字label
    lazy var textLabel: UILabel = UILabel(text: "登录后,别人评论你的微博,给你发送消息,你都会收到", textAlignment: .Center)
    
    //注册按钮
    private lazy var registButton: UIButton = UIButton(title: "注册", titleColor: UIColor.orangeColor(), font: 14, backGroundImage: "common_button_white_disable", target: self, action: "regist:")
    
    //登录按钮
    private lazy var loginButton: UIButton = UIButton(title: "登录", titleColor: UIColor.darkGrayColor(), font: 14, backGroundImage: "common_button_white_disable", target: self, action: "login:")
    
    //访客视图的构造函数
    init() {
        
        super.init(frame: screenBounds)
        setupUI()
    }
    
    //required，如果说父类定义一个函数，使用 required修饰，则子类必须重写该方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension VisitorView {
    
    func setVisitorView(title: String, icon: String, isHome: Bool = false) {
        
        textLabel.text = title
        iconView.image = UIImage(named: icon)
        
        if isHome {
            
            circleView.hidden = false
            maskIconView.hidden = false
        }
    }
    
    /**
     添加转圈动画
     */
    private func addAnimation() {
        
        //transform.rotation 代表旋转
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        //动画执行一次的时间
        animation.duration = 15
        
        //2 * M_PI 代表360度旋转
        animation.toValue = 2 * M_PI
        
        //动画重复次数
        animation.repeatCount = MAXFLOAT
        
        //是否执行完成之后移除动画
        animation.removedOnCompletion = false
        
        //将动画添加到view上
        circleView.layer.addAnimation(animation, forKey: nil)
    }
    
    //自动布局，添加子控件，给子控件设置初始值
    private func setupUI() {
        
        backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)
        
        //将控件添加到访客视图上
        addSubview(iconView)
        addSubview(circleView)
        addSubview(maskIconView)
        addSubview(textLabel)
        addSubview(registButton)
        addSubview(loginButton)
        
        //对于多数而言，如果设置相同，应该设为默认值，而只对少数做处理
        circleView.hidden = true
        maskIconView.hidden = true
        
        //自动布局
        //masonry是oc当中自动布局的框架。snapkit，是masonry公司出的，专门用于swift自动布局的框架。
        //1. 自动布从上到下，从左到右依次布局
        //2. 每写完一个控件的自动布局，需要运行一下，看看效果，不要所有控件全部写完再看效果。
        //3. 对于可能被拉伸或者宽高可能动态调整的控件，如果将其作为参照，需要特别留意。
        //4. 不要忘了写参照控件的参照属性
        
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-100)
        }
        
        circleView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(iconView)
        }
        
        //maskIconview有可能被拉伸
        maskIconView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(circleView)
            make.centerY.equalTo(circleView).offset(45)
            make.width.equalTo(self)
        }
        
        //textLabel有可能是多行
        textLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(circleView)
            make.centerY.equalTo(circleView.snp_bottom).offset(50)
            make.left.equalTo(self).offset(50)
            make.right.equalTo(self).offset(-50)
        }
        
        registButton.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(self).offset(80)
            make.centerY.equalTo(circleView.snp_bottom).offset(120)
            make.width.equalTo(100)
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            
            make.right.equalTo(self).offset(-80)
            make.centerY.equalTo(circleView.snp_bottom).offset(120)
            make.width.equalTo(100)
        }
        
        //添加circleView的动画
        addAnimation()
    }
}

// MARK: - 处理事件
extension VisitorView {
    
    @objc func regist(button: UIButton) {
        
        print("注册")
    }
    
    @objc func login(button: UIButton) {
        
        print("登录")
        delegate?.didLogin()
    }
}

